$(function(){
	var walletType=sessionStorage.getItem("walletType");
	var logonToken=sessionStorage.getItem("logonToken");
	var playId=sessionStorage.getItem("playId");
	
	
//	公告
	var json = {
        "logonToken": logonToken,
        "walletType": walletType,
        "playId":playId
    }
    $.ajax({
        type: "post",
        url: url + '/bigWinner/api/login/freshen',
        data: JSON.stringify(json),
        dataType: 'json',
        contentType: "application/json",
        success: function(result) {
               	var noticList_html='';
               	var noticList=result.noticList;
				for(var i in noticList){
					noticList_html += '<div class="con_t1_msg"><h3>'+noticList[i].title+'</h3><p>'+noticList[i].addTime+'</p>';
					noticList_html += '<p>'+noticList[i].content+'</p></div>';
				}
				if(noticList_html == ""){
					noticList_html = "<img src='img/noticList.png' style='width:100%;'>"; 	
					$(".con_t1").css("marginBottom","0");
				}
				$(".noticList_list").append(noticList_html);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
           alert("请求失败："+XMLHttpRequest.status);
        }
    });
	
//	获取最新活动
function ooo(){
	var json = {
        "logonToken": logonToken,
        "walletType": walletType,
        "playId":playId
    }
    $.ajax({
        type: "post",
        url: url + '/bigWinner/api/donation/donationPage',
        data: JSON.stringify(json),
        dataType: 'json',
        contentType: "application/json",
        success: function(result) {
            if (result.success) {
//				活动列表
				var activityList = result.obj.allActivity;
				var activityList_html='<option value="">请选择你要参与的活动</option>';
				for(var i in activityList){
					activityList_html += '<option value="'+activityList[i].id+'">'+activityList[i].activityName+'</option>';
				}
				$(".sel_hd").html(activityList_html);
				
				//	最新活动
				var record = result.obj.allActivity;
				if(record == "" || record == null || record == undefined){
					$(".sel_row").css("opacity","0");
					$(".cutBtn").find("img").css("display","none");
				}else{
					record = result.obj.allActivity;
					if($(".sel_hd option").length > 0){
						$(".sel_hd option").eq(1).attr("selected","selected");
					}
					$(".activityName").html(record[0].activityName);
					$(".nowNum").html("第"+record[0].nowNum+"轮");
					$(".nowTargetAmount").html(record[0].nowTargetAmount);
					$(".nowRaisedAmount").html(record[0].nowRaisedAmount);
					if(isNaN(record[0].nowRaisedAmount/record[0].nowTargetAmount)){
						
					}else{
						$(".progress .active").css("width",(record[0].nowRaisedAmount/record[0].nowTargetAmount)*100+"%").html((record[0].nowRaisedAmount/record[0].nowTargetAmount)*100+"%");
					}
					if(record[0].nowStatus == "0"){
						$(".nowStatus").html("进行中");
					}else{
						$(".nowStatus").html("已结束");
					}
//					复投判断1
					var  pattern=result.obj.pattern;
					if(pattern == 2 || pattern == null){
						$('.dataCut').attr('data-cut', '');
				        $('.dataCut').html('未参加当前活动');
					}else if(pattern == 1){
						$('.dataCut').attr('data-cut', '1');
				        $('.dataCut').html('自动复投');
					}else if(pattern == 0){
						$('.dataCut').attr('data-cut', '0');
				        $('.dataCut').html('手动复投');
					}
//					复投判断2:只有当这两个状态都在1时,才能更新复投状态
					if(pattern == 2 || pattern == null){
						$(".cutBtn").find("img").css("display","none");
					}else if(record.nowStatus == "0" && record.status == "1"){
						$(".cutBtn").find("img").css("display","inline-block");
					}else{
						$(".cutBtn").find("img").css("display","none");
					}
					
					$(".sel_row").css("opacity","1");
				}
               
            } else {
                isErr(result);
                $(".sel_row").css("opacity","0");
				$(".cutBtn").find("img").css("display","none");
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("请求失败："+XMLHttpRequest.status);
        }
    });
}
	
	
	
//	个人资产

function ger_zc(){
	var json = {
        "logonToken": logonToken,
        "walletType": walletType,
        "playId":playId
    }
    $.ajax({
        type: "post",
        url: url + '/bigWinner/api/login/refreshAccount',
        data: JSON.stringify(json),
        dataType: 'json',
        contentType: "application/json",
        success: function(result) {
        	var data=result.obj.account;
            $(".available_balance").html(data.availableBalance);
			$(".invest").html(data.invest);
			$(".returned").html(data.returned);
			$(".frozen").html(data.frozen);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("请求失败："+XMLHttpRequest.status);
        }
    });
}
	ger_zc();
	ooo();
	
	
//	获取数据
function hd_hh(){
	var json = {
        "logonToken": logonToken,
        "walletType": walletType,
        "playId":playId
    }
    $.ajax({
        type: "post",
        url: url + '/bigWinner/api/activity/playActivityPage',
        data: JSON.stringify(json),
        dataType: 'json',
        contentType: "application/json",
        success: function(result) {
            if (result.success) {
               var html ='<option value="" data="">请选择你要参与的活动</option>';
               var data=result.obj;
               for(var i in data){
               	html += '<option value="'+data[i].id+'" data="'+data[i].maxNum+'">'+data[i].activityName+'</option>';
               }
               $(".sel_hd_1").html(html);
               $(".sel_hd_1 option").eq(1).attr("selected","selected");
               if(data[0] == null || data[0] == undefined || data[0] == ""){
               		$(".modal_h6").css("display","none")
               }else{
               		$(".maxNum").html(data[0].maxNum);
               }
               
            } else {
                isErr(result);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("请求失败："+XMLHttpRequest.status);
        }
    });
}
 	hd_hh();
    
//  参与活动数量
	$(".investmentAmount").on("keyup",function(){
		if(Number($(this).val()) > Number($(".maxNum").html())){
			alert("当前可输入的最大数量为"+$(".maxNum").html());
			var s=$(this).val();
			s=s.substring(0,s.length-1);
			$(this).val(s);
		}
	})
    
//  弹框切换
	$(".sel_hd_1").on("change",function(){
		var kk=$(".sel_hd_1 option:selected").attr("data");
		if(kk == ""){
			$(".modal_h6").css("display","none");
		}else{
			$(".modal_h6").css("display","block");
			$(".maxNum").html(kk);
		}
	})
    
//  切换活动
	$(".sel_hd").on("change",function(){
		var type=$(".sel_hd option:selected").val();
		var json = {
	        "logonToken": logonToken,
	        "walletType": walletType,
	        "playId":playId,
	        "activityId":type
	    }
	    $.ajax({
	        type: "post",
	        url: url + '/bigWinner/api/donation/select',
	        data: JSON.stringify(json),
	        dataType: 'json',
	        contentType: "application/json",
	        success: function(result) {
	            if (result.success) {
	            	var data=result.obj.activityRecord;
	              	$(".activityName").html(data.activityName);
					$(".nowNum").html("第"+data.nowNum+"轮");
					$(".nowTargetAmount").html(data.nowTargetAmount);
					$(".nowRaisedAmount").html(data.nowRaisedAmount);
					$(".dataCut").attr("data_id",data.id);
					if(isNaN(data.nowRaisedAmount/data.nowTargetAmount)){
						
					}else{
						$(".progress .active").css("width",(data.nowRaisedAmount/data.nowTargetAmount)*100+"%").html((data.nowRaisedAmount/data.nowTargetAmount)*100+"%");
					}
					if(data.nowStatus == "0"){
						$(".nowStatus").html("进行中");
					}else{
						$(".nowStatus").html("已结束");
					}
					
//					复投方式
					var pattern1=result.obj.pattern;
					if(pattern1 == 2 || pattern1 == null){
						$('.dataCut').attr('data-cut', '');
				        $('.dataCut').html('未参加当前活动');
					}else if(pattern1 == 1){
						$('.dataCut').attr('data-cut', '1');
				        $('.dataCut').html('自动复投');
					}else if(pattern1 == 0){
						$('.dataCut').attr('data-cut', '0');
				        $('.dataCut').html('手动复投');
					}
					if(pattern1 == 2 || pattern1 == null){
						$(".cutBtn").find("img").css("display","none");
					}else if(data.nowStatus == "0" && data.status == "1"){
						$(".cutBtn").find("img").css("display","inline-block");
					}else{
						$(".cutBtn").find("img").css("display","none");
					}
					$(".sel_row").css("opacity","1");
	            } else {
	                isErr(result);
	                $(".sel_row").css("opacity","0");
					$(".cutBtn").find("img").css("display","none");
	            }
	        },
	        error: function(XMLHttpRequest, textStatus, errorThrown) {
	            alert("请求失败："+XMLHttpRequest.status);
	        }
	    });
	})
    
    var prevent_repetition_1 = true;
    $(".bound_btn").on("click",function(){
    	if (prevent_repetition_1) {
            prevent_repetition_1 = false;
            var investmentAmount = $(".investmentAmount").val();
            var id = $(".sel_hd_1 option:selected").val();
            var pattern2 = $(".sel_h option:selected").val();
            var payPassword = $("#recipient-name1").val();
            if(id == ""){
            	alert("请选择要参与的活动");
            	prevent_repetition_1 = true;
                return false;
            }
            if (!investmentAmount) {
                alert("请输入数量");
                prevent_repetition_1 = true;
                return false;
            }
            if (!payPassword) {
                alert("请输入支付密码");
                prevent_repetition_1 = true;
                return false;
            }
            var json = {
                "logonToken": logonToken,
		        "walletType": walletType,
		        "playId":playId,
                "investmentAmount": investmentAmount,
                "id": id,
                "pattern": pattern2,
                "payPassword":payPassword
            }
            $.ajax({
                type: "post",
                url: url + '/bigWinner/api/activity/play',
                data: JSON.stringify(json),
                dataType: 'json',
                contentType: "application/json;charset=UTF-8",
                success: function(result) {
                    if (result.success) {
                    	alert("参与成功");
//                  	更新个人信息
                    	ger_zc();
                    	ooo();
						hd_hh();
						$("#exampleModal").modal("toggle");
                    } else {
                        isErr(result);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert("请求失败："+XMLHttpRequest.status);
                },
                complete: function() {
                    prevent_repetition_1 = true;
                }
            });
        }
    })
    
//  切换复投模式
	$(".cutBtn").on("click","img",function(){
		if (prevent_repetition_1) {
            prevent_repetition_1 = false;
            var activityId=$(".dataCut").attr("data_id");
            var json = {
                "logonToken": logonToken,
		        "walletType": walletType,
		        "playId":playId,
                "activityId": activityId
            }
            $.ajax({
                type: "post",
                url: url + '/bigWinner/api/donation/switchModel',
                data: JSON.stringify(json),
                dataType: 'json',
                contentType: "application/json;charset=UTF-8",
                success: function(result) {
                    if (result.success) {
                    	alert(result.msg);
                    	var cut = $('.dataCut').attr('data-cut');
				      	if (cut == 1) {
				      		var cut=0;
				          	$('.dataCut').attr('data-cut', '0');
				         	$('.dataCut').html('手动复投');
				      	}else {
				      		var cut=1;
				         	$('.dataCut').attr('data-cut', '1');
				       	   	$('.dataCut').html('自动复投');
				      	}
                    } else {
                        isErr(result);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert("请求失败："+XMLHttpRequest.status);
                },
                complete: function() {
                    prevent_repetition_1 = true;
                }
            });
		}
	})
    
    
})
